package com.renjibo.teawithmilkgoods;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renjibo.teawithmilkgoods.Model.GoodsBean;
import com.renjibo.teawithmilkgoods.fragments.CartFragment;
import com.renjibo.teawithmilkgoods.fragments.HomeFragment;
import com.renjibo.teawithmilkgoods.fragments.MyFragment;
import com.renjibo.teawithmilkgoods.fragments.TopicFragment;
import com.renjibo.teawithmilkgoods.views.NewsFragmentTabHost;
import com.renjibo.teawithmilkgoods.views.TitleBar;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MainActivity extends AppCompatActivity {
    private NewsFragmentTabHost mfragmentTabHost;
    private String name[]={
        "月光茶人","优惠","购物车","我的"
    };
    private int images[]={R.drawable.home_image,R.drawable.topic_image,R.drawable.normal_image,R.drawable.my_image};
    private Class fragments[]={HomeFragment.class, TopicFragment.class, CartFragment.class, MyFragment.class};
    private TitleBar titleBar;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gson = new Gson();
        initView();
        //postHttpJson();

    }
    //请求网络数据
    public void postHttpJson(){
        RequestParams params = new RequestParams("http://eleteamapi.ygcr8.com/v1/category/list-with-product");
        //params.addQueryStringParameter("wd", "xUtils");
        x.http().post(params, new Callback.CommonCallback<String>(){

            @Override
            public void onSuccess(String result) {
                GoodsBean goodsBean = gson.fromJson(result, GoodsBean.class);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    public void initView(){
        titleBar = (TitleBar) findViewById(R.id.titleBar);
        mfragmentTabHost = (NewsFragmentTabHost) findViewById(R.id.tabhost);
        mfragmentTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        mfragmentTabHost.getTabWidget().setDividerDrawable(null); // 去掉分割线

        for(int i=0;i<images.length;i++){
            TabHost.TabSpec tabSpec=mfragmentTabHost.newTabSpec(name[i]).setIndicator(getImageView(i));
            //添加Fragment
            mfragmentTabHost.addTab(tabSpec,fragments[i],null);

            final int finalI = i;
            mfragmentTabHost.getTabWidget().getChildTabViewAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    titleBar.setTitle(name[finalI]);
                    mfragmentTabHost.setCurrentTab(finalI);
                }
            });
            mfragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.pedo_actionbar_bkg);
        }
        /**
         * 解决冲突问题
         */

    }
    public View getImageView(int index){
        View bottom_view= getLayoutInflater().inflate(R.layout.bottom_setview,null);
        ImageView bottom_imageView= (ImageView) bottom_view.findViewById(R.id.bottom_view_imageView);
        TextView bottom_view_textView= (TextView) bottom_view.findViewById(R.id.bottom_view_textView);
        bottom_imageView.setImageResource(images[index]);
        bottom_view_textView.setText(name[index]);
        return bottom_view;
    }
}
